
// 微信小游戏适配器完整版
// 参考官方实现：https://res.wx.qq.com/wechatgame/plugin/weapp-adapter/weapp-adapter.js

(function() {
  if (typeof global === 'undefined') {
    global = {};
  }

  // 定义Game构造函数
  global.Game = function(config) {
    const game = {
      canvas: null,
      ctx: null,
      onLaunch: function() {},
      onShow: function() {},
      onHide: function() {},
      onError: function() {}
    };

    // 合并配置
    Object.assign(game, config);

    // 初始化游戏
    wx.onShow(() => {
      if (!game.canvas) {
        game.canvas = wx.createCanvas();
        game.ctx = game.canvas.getContext('2d');
        game.onLaunch();
      }
      game.onShow();
    });

    wx.onHide(() => {
      game.onHide();
    });

    wx.onError((error) => {
      game.onError(error);
    });

    return game;
  };

  // 性能API适配
  if (!global.performance) {
    global.performance = {
      now: function() {
        return Date.now();
      }
    };
  }

  // requestAnimationFrame适配
  if (!global.requestAnimationFrame) {
    global.requestAnimationFrame = (function() {
      return wx.requestAnimationFrame ||
             wx.createSelectorQuery().selectViewport().boundingClientRect().exec.bind(null);
    })();
  }

  // cancelAnimationFrame适配
  if (!global.cancelAnimationFrame) {
    global.cancelAnimationFrame = wx.cancelAnimationFrame;
  }

  // 其他API适配...
  console.log('weapp-adapter initialized');
})();